﻿var wjx_domain = 'http://wjx.711688.com';       //主域名
var shared_domain = 'http://wjx2.711688.net';    //分享域名(.net朋友圈底部广告被遮住,.cn域名空间被屏蔽)
//shared_domain = 'http://192.168.1.162:8011';
shared_domain = "http://192.168.1.162:8011"
var filter_url = ["toutiao.aspx", "weixin.aspx","qq.aspx"]; //内部页地址,详细页用到

$(document).ready(function () {
    if ("wap" == getUrlArgStr("sources")) {
        $(".wamtop").css("display", "none");
    }

});

var token = getCookie("token");
var userid = getCookie("userid");

/*获取sources中的参数值*/
function getUrlArgStr(values) {
    var q = location.search.substr(1);
    var qs = q.split('&');
    var argStr = '';
    if (qs) {
        for (var i = 0; i < qs.length; i++) {
            if (values == qs[i].substring(0, qs[i].indexOf('='))) {
                argStr = qs[i].substring(qs[i].indexOf('=') + 1);
                break;
            }
        }
    }
    return argStr;
}

/*获取问好传值*/
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = decodeURI(window.location.search).substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

function islogin() {
    token = getCookie("token");
    userid = getCookie("userid");
    if (userid == undefined || userid == "" || userid == null) {
        return false;
    }
    return true;
}


/* 跳转页面 */
function toUrl(url) {
    window.location.href = url;
}



//删除左右两端的空格
function trim(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
}
//显示消息
function Alert(msg) {
    toast.show(msg);
    //alert(msg);
}
//设置cookies
function SetCookie(name, value, Days) {
    if (Days == null) Days = 30;
    //var Days = 30; //设置Cookie有效时间
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
}
//获取cookies
function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]); return null;

}
//删除cookies
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) {
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/";
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }
}

/* 缓存到磁盘 */
function setStorage(key, value) {
    localStorage.setItem(key, value);
}

// 读取缓存到磁盘的数据
function getStorage(key) {
    return localStorage.getItem(key);
}

// 删除缓存
function delStorage(key) {
    if (key != null) {
        localStorage.removeItem(key);
    } else {
        localStorage.clear();
    }
}

/* 缓存到磁盘 */
function setSession(key, value) {
    sessionStorage.setItem(key, value);
}

// 读取缓存到磁盘的数据
function getSession(key) {
    return sessionStorage.getItem(key);
}

// 删除缓存
function delSession(key) {
    if (key != null) {
        sessionStorage.removeItem(key);
    } else {
        sessionStorage.clear();
    }
}

//网址验证
function IsURL(url) {
    var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
         + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@  
         + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184  
         + "|" // 允许IP和DOMAIN（域名） 
         + "([0-9a-z_!~*'()-]+\.)*" // 域名- www.  
         + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名  
        + "[a-z]{2,6})" // first level domain- .com or .museum  
        + "(:[0-9]{1,4})?" // 端口- :80  
        + "((/?)|" // a slash isn't required if there is no file name  
        + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
    var re = new RegExp(strRegex);
    if (re.test(url)) {
        return (true);
    } else {
        return (false);
    }
}
function CheckUrl(str) {
    if (str.indexOf("http") > -1 || str.indexOf(".com") > -1 || str.indexOf(".cn") > -1 || str.indexOf(".cc") > -1 || str.indexOf(".con") > -1)
        return true;
    return false;
}

//返回
function goBack() {
    var refurl = document.referrer.toString().toLocaleLowerCase();
    var host = window.location.host;
    //登录页跳转到发现首页
    if (refurl.indexOf("personal") > -1 || refurl.indexOf("makesh_1") > -1) {
        if (getCookie("userid") == null) {
            window.location.href = "index.html";
            return
        }
    }
    if (refurl.indexOf("Selectshare") > -1 || refurl.indexOf("Record") > -1 || refurl.indexOf("Statistical") > -1 || refurl.length == 0 || refurl.indexOf(host) == -1) {
        window.location.href = "index.html";
    }
    else {
        window.location.href = refurl;
    }
}

// 仿java
String.prototype.startWith = function (str) {
    if (str == null || str == "" || this.length == 0
			|| str.length > this.length)
        return false;
    if (this.substr(0, str.length) == str)
        return true;
    else
        return false;
    return true;
}

// 仿java
String.prototype.endWith = function (str) {
    if (str == null || str == "" || this.length == 0
			|| str.length > this.length)
        return false;
    if (this.substring(this.length - str.length) == str)
        return true;
    else
        return false;
    return true;
}




var _logintype = 0, _user_id = 0;
function GoPage(logintype, userid, token, parentid) {
    SetCookie("token", token);
    SetCookie("userid", userid);
    SetCookie("logintype", logintype);
    SetCookie("parentid", parentid);
    if (IsMobile() < 1) {
        Go(logintype);  //网页登录
        return;
    }
    _logintype = logintype;
    _user_id = userid;
    SetClient();    //设置推送    
}

function Go(logintype) {
    if (logintype > 1) {
        window.location.href = "personal.html";
        return;
    }
    var ref = document.referrer.toString();
    if (ref != "" && ref.indexOf("login.html") == -1 && ref.indexOf("reset-pwd.html") == -1 && ref.indexOf("login-old.html") == -1 && ref.indexOf("regist.html") == -1 && ref.indexOf("registered.html") == -1 && ref.indexOf("Forgot.html") == -1 && ref.indexOf("Set.html") == -1)
        window.location.href = document.referrer.toString();
    else
        window.location.href = "personal.html";
}

//设置推送
function SetClient() {   
    if (navigator.userAgent.indexOf('wjx2.') > -1) {       
        var param = { callback: "Call_SetClient" };
        window.location.href = "callback://pushInfo?" + JSON.stringify(param);
    }
    else {
        Go(_logintype); //1.0版本登录
    }
}

function Call_SetClient(channelid, userId, store) {
    var clientid = IsMobile();
    if (store == null) store = 0;
    var param = { "clientid": clientid, "channelid": channelid, "userid": userId, "user_id": _user_id, "store": store };
  
    if (param.channelid == null) {
        //alert("推送加载异常");  //暂时注释不显示错误提示
        Go(_logintype);
        return;
    }
    $.post("ajax/push.ashx?setclient", param, function (json) {
        if (json.code == 0)
            SetLocation();  //设置区域
        else {
            alert(json.msg);
            Go(_logintype);
        }
    },
     "json");
}

//设置区域
function SetLocation() {
    var param = { callback: "Call_Location", isRefresh: false };
    var p = JSON.stringify(param);
    window.location.href = "callback://location?" + p;
}

function Call_Location(jsonStr) {
    var json = jsonStr;
    if (json != null) {
        json = JSON.parse(json);
        var area1 = json.province;
        var area2 = json.city;
        if (area1.substring(area1.length - 1) == '省')
            area1 = area1.substring(0, area1.length - 1);

        if (area2.substring(area2.length - 1) == "市")
            area2 = area2.substring(0, area2.length - 1);
        
        SetCookie('area', area1 + "," + area2 + "," + json.district);
        Go(_logintype); //设置定位后，跳转到会员中心
        return;
    }
    else
        Go(_logintype);
}

function IsMobile() {
    var ua = navigator.userAgent.toLocaleLowerCase();
    if (ua.indexOf("wjx") > -1) {
        if (ua.indexOf("android") > -1) return 1;
        if (ua.indexOf("iphone") > -1) return 2;
    }
    return 0;
}

// 登录状态验证
function login() {
    if (!islogin()) {
        window.location.href = 'login.html';
        return false;
    }
    return true;
}


//第一次显示步骤
function ShowBZ(i) {
    return;
    if (IsMobile() < 1) return;
    setTimeout(function () {
        var bz = getStorage("bz");
        if (bz == null) bz = "";
        if (bz.indexOf("," + i + "") == -1 && bz.indexOf("," + i + ",") == -1) {
            $(".buzhou").eq(0).removeClass("hide");
            $("body").css("overflow", "hidden");
        }
    }, 1500)
}

//上一步引导
function UpBZ(i) {
    $(".buzhou").addClass("hide");
    $(".buzhou").eq((i - 1)).removeClass("hide");
}
//下一步引导
function NextBZ(i) {
    $(".buzhou").addClass("hide");
    $(".buzhou").eq(i).removeClass("hide");
}

//保存引导阅读状态
function SetBZ(name) {
    var bz = getStorage("bz");
    if (bz == null) bz = "";
    if (bz.indexOf(name) == -1)
        setStorage("bz", bz + "," + name);
    $(".buzhou").addClass("hide");
    $("body").css("overflow", "auto");
}

//是否重复抢红包
function checkHB(id) {
    var old =getCookie("fcid");
    if (old == null) return true;
    old = decodeURIComponent(old);
    if (old.indexOf(id + ",") > -1 || old.indexOf("," + id) > -1)
        return false;
    return true;
}
function SetHB(id) {
    var old = getCookie("fcid");
    if (old == null) old = "";
    if (checkHB(id))
        SetCookie("fcid", old + id + ",", 5);
}

//微信视频宽、高(粘贴外链用到)
function reloadWxVideo() {
    var iframeDoc = $("#ifmUrl").contents();
    var videoFrame = $(iframeDoc).find("iframe");

    //判断是否视频
    if (videoFrame.length > 0) {

        $(videoFrame).each(function () {
            var videoUrl = $(this).attr("src");
            $($(this)).removeAttr("src");

            //提取视频id
            var vid = videoUrl.substring(videoUrl.indexOf("vid="))
            vid = vid.substring(4, vid.indexOf("&"));

            //视频页面 不含参数
            var videoHost = videoUrl.substring(0, videoUrl.indexOf("?")).replace("preview", "player");

            //修改后的视频宽度
            var vw = $(window).width() - 30;

            //修改后的视频高度
            var vh = vw / 500 * 375

            //设置兼容微信视频
            videoUrl = videoHost + "?vid=" + vid + "&width=" + vw + "&height=" + vh + "&auto=0"
            $(this).attr("src", videoUrl);

            //解决超出的空白
            $(this).css("width", vw + "px");

            //解决超高空白
            $(this).css("height", vh + "px");

            $(this).attr("width", vw);
            $(this).attr("height", vh);
        });
    }
}